package com.sx.controller;

import com.sx.service.GoodService;
import com.sx.service.serviceImpl.GoodServiceImpl;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.Serial;

@WebServlet("/checkGoodIdExists.action")
public class checkIdExists extends HttpServlet {
    @Serial
    private static final long serialVersionUID = 1L;
    // 创建一个GoodService实例
    private final GoodService goodsService = new GoodServiceImpl();
    // 创建一个日志记录器
    private final Logger logger = LogManager.getLogger(checkIdExists.class);

    /**
     * 处理HTTP POST请求的方法
     * 该方法用于检查商品ID是否存在重复
     *
     * @param req  用于获取请求参数的HttpServletRequest对象
     * @param resp 用于发送响应的HttpServletResponse对象
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
        // 从请求参数中获取商品ID
        String id = req.getParameter("id");
        boolean exists;
        // 调用goodsService的checkIdExists方法检查商品ID是否存在重复
        // 尝试通过学生代码和密码查找用户如果返回null，则表示没有重复的用户，用户名可用
        try {
            exists = goodsService.checkIdExists(id);
            resp.getWriter().write(String.valueOf(exists));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}
